//
// Created by root on 4/29/17.
//


#include "../runner.h"

static void test_extend_environment() {
    Cell* frame = make_vector(global_scheme, DEFAULT_FRAME_SIZE);
    extend_environment(global_scheme, frame, global_scheme->env);
    Cell* f = first(global_scheme->env);
    assert_ptr_equal(global_scheme->env, global_scheme->globalEnv);
    assert_ptr_equal(f, frame);
}

static void test_add_binding_to_frame() {
    Cell* frame = make_vector(global_scheme, DEFAULT_FRAME_SIZE);
    add_binding_to_frame(global_scheme, frame, "abc", make_integer(global_scheme, 123), FALSE);
    add_binding_to_frame(global_scheme, frame, "def", make_string(global_scheme, "456"), FALSE);
    Cell** items = get_all_vector_items(frame);
    while (*items != NULL) {
        Cell* slot = car(*items);
        printf("var: %s\n", cell2str(global_scheme, car(slot)));
        printf("val: %s\n", cell2str(global_scheme, cdr(slot)));
        items++;
    }
}

static void test_lookup_variable_value_global() {
    Cell* frame = make_vector(global_scheme, DEFAULT_FRAME_SIZE);
    add_binding_to_frame(global_scheme, frame, "abc", make_integer(global_scheme, 123), FALSE);
    add_binding_to_frame(global_scheme, frame, "def", make_string(global_scheme, "456"), FALSE);
    Cell* abc = lookup_variable_value(global_scheme, global_scheme->env, "abc");
    Cell* def = lookup_variable_value(global_scheme, global_scheme->env, "def");
    printf("abc: %s\n", cell2str(global_scheme, abc));
    printf("def: %s\n", cell2str(global_scheme, def));
}

static void test_lookup_variable_value_current() {
    ;
}

static void test_lookup_variable_value_none() {
    Cell* frame = make_vector(global_scheme, DEFAULT_FRAME_SIZE);
    add_binding_to_frame(global_scheme, frame, "abc", make_integer(global_scheme, 123), FALSE);
    add_binding_to_frame(global_scheme, frame, "def", make_string(global_scheme, "456"), FALSE);
}

int main(int argc, char **argv) {
    const struct CMUnitTest tests[] = {
            cmocka_unit_test(test_extend_environment),
            cmocka_unit_test(test_add_binding_to_frame),
            cmocka_unit_test(test_lookup_variable_value_global),
    };
    return cmocka_run_group_tests(tests, setup_scheme, teardown_scheme);
}
